package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.workspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class workspaceServiceImpl implements workspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询今日营业额
     * @return
     */
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {

        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end",end);
        //统计当日订单总数
        Integer totalOrders = orderMapper.getCount(map);

        map.put("status", Orders.COMPLETED);
        //统计当然订单有效数
        Integer validOrderCount = orderMapper.getCount(map);
        //营业额统计
        Double turnover = orderMapper.sumByMap(map);

        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;
        if (totalOrders != 0){
            orderCompletionRate = (double) validOrderCount / totalOrders ;
            unitPrice = (double) turnover / validOrderCount ;
        }

        Integer newUsers = userMapper.getUserByTime(map);

        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .newUsers(newUsers)
                .unitPrice(unitPrice)
                .build();

        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     * @return
     */
    public OrderOverViewVO overviewOrders() {
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);

        //统计待接单数
        Integer waitingOrders = orderMapper.getCount(map);

        //待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.getCount(map);

        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.getCount(map);

        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.getCount(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.getCount(map);

        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();

        return orderOverViewVO;
    }

    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO overviewDishes() {

        //统计启售的数量
        Integer sold = dishMapper.getCount(StatusConstant.ENABLE);

        //统计停售的数量
        Integer discontinued = dishMapper.getCount(StatusConstant.DISABLE);

        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();

        return dishOverViewVO;
    }

    /**
     * 查询套餐总览
     * @return
     */
    public SetmealOverViewVO overviewSetmeals() {

        //统计启售的套餐
        Integer sold = setmealMapper.getCount(StatusConstant.ENABLE);

        //统计停售的套餐
        Integer discontinued = setmealMapper.getCount(StatusConstant.DISABLE);

        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();

        return setmealOverViewVO;
    }
}
